package smartcampus.scjava.controller.forth;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import smartcampus.scjava.POJO.DO.Equipment;
import smartcampus.scjava.POJO.DO.Laboratory;
import smartcampus.scjava.custom.ResultVO;
import smartcampus.scjava.service.ILaboratoryService;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 实验室表 前端控制器
 * </p>
 *
 * @author xian
 * @since 2023-12-05
 */
@RestController
@RequestMapping("/laboratory")
public class LaboratoryController {

    @Resource
    private ILaboratoryService laboratoryService;

    @RequestMapping("/listPage")
    public ResultVO<?> listPageBook(Integer  pageNum, Integer pageSize, String word) {
        if (pageNum == null || pageSize == null) {
            return ResultVO.error("分页参数错误");
        }

        Page<Laboratory> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Laboratory> query = new QueryWrapper<>();
        if (StringUtils.hasText(word)) {
            query.lambda().like(Laboratory::getLaboratoryNo,word).
                    or().like(Laboratory::getLaboratoryName,word).
                    or().like(Laboratory::getLocation,word);
        }

        laboratoryService.page(page,query);
        return ResultVO.success(page);
    }


    @RequestMapping("/add")
    public ResultVO<?> addLaboratory(@RequestBody Laboratory laboratory) {

        Laboratory byNo = laboratoryService.getByNo(laboratory.getLaboratoryNo());
        if (byNo != null) {
            return ResultVO.error("实验室编号已存在");
        }
        laboratory.setCreateTime(LocalDateTime.now());
        laboratory.setUpdateTime(LocalDateTime.now());
        laboratoryService.save(laboratory);
        return ResultVO.success("添加成功！");
    }

    @RequestMapping("/update")
    public ResultVO<?> updateLaboratory(@RequestBody Laboratory laboratory) {

        Laboratory byNo = laboratoryService.getByNo(laboratory.getLaboratoryNo());
        if (byNo != null && !byNo.getId().equals(laboratory.getId())) {
            return ResultVO.error("实验室编号已存在");
        }
        laboratoryService.updateById(laboratory);
        return ResultVO.success("修改成功！");
    }

    @RequestMapping("/delete")
    public ResultVO<?> deleteLaboratory(Integer id) {
        laboratoryService.removeById(id);
        return ResultVO.success("删除成功！");
    }
}
